package com.example.smartgreenhouse.service.impl;

import com.example.smartgreenhouse.entity.SoilMoisture;
import com.example.smartgreenhouse.mapper.SoilMoistureMapper;
import com.example.smartgreenhouse.service.SoilMoistureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Configuration
public class SoilMoistureServiceImpl implements SoilMoistureService {
    private final SoilMoistureMapper soilMoistureMapper;
    @Autowired
    public SoilMoistureServiceImpl(SoilMoistureMapper soilMoistureMapper) {
        this.soilMoistureMapper = soilMoistureMapper;
    }

    @Override
    public Map<String, Object> saveSoilMoisture(int soilMoistureValue,int changeValue, float changeRate,LocalDateTime collectionTime) {
        Map<String, Object> response = new HashMap<>();
        // 创建 SoilMoisture 对象并设置数据
        SoilMoisture soilMoisture = new SoilMoisture();
        soilMoisture.setSensor_id(1L);
        soilMoisture.setGreenhouse_id(1L);
        soilMoisture.setMoisture(soilMoistureValue);  //设置当前湿度值
        soilMoisture.setMoisture_change_value(changeValue);
        soilMoisture.setMoisture_change_rate(changeRate);
        soilMoisture.setCollect_time(collectionTime);  // 设置数据采集时间
        soilMoisture.setCreate_time(LocalDateTime.now()); // 设置数据创建时间
        // 将数据插入到数据库
        soilMoistureMapper.insert(soilMoisture); // 使用 MyBatis-Plus 插入数据
        // 返回成功响应
        response.put("status", "success");
        response.put("message", "Data saved successfully.");
        response.put("timestamp", LocalDateTime.now());
        return response;
    }
}
